package com.jt.assistant.ui

import android.content.Intent
import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import com.afollestad.materialdialogs.MaterialDialog
import com.afollestad.materialdialogs.lifecycle.lifecycleOwner
import com.afollestad.materialdialogs.list.listItems
import com.blankj.utilcode.util.ToastUtils
import com.jt.assistant.R
import com.jt.assistant.adapter.CarSelfAdapter
import com.jt.assistant.bean.Param
import com.jt.assistant.bean.ReceiveWarePlan
import com.jt.assistant.viewmodel.HomeViewModel
import com.yzp.mvvmlibrary.base.BaseActivity
import kotlinx.android.synthetic.main.activity_car_self.*
import kotlinx.android.synthetic.main.activity_car_self.top_bar
import kotlinx.android.synthetic.main.activity_car_self.tv_car_time

/**
 * 送货上门计划查询
 */
class CarSelfActivity : BaseActivity<HomeViewModel>() {

    private val mAdapter by lazy {
        CarSelfAdapter(
            ArrayList<ReceiveWarePlan>()
        )
    }
    private var mDay = "1"


    override fun layoutId(): Int = R.layout.activity_car_self

    override fun initView(savedInstanceState: Bundle?) {
        with(rv_carSelf) {
            layoutManager = LinearLayoutManager(context)
            adapter = mAdapter
        }
    }

    override fun initData() {
        val params = Param::class.java.newInstance()
        params.day = mDay
        viewModel.getCarPlanList(params)
    }

    override fun initListener() {


        tv_car_time.setOnClickListener {
            showTimeListDialog()
        }

        mAdapter.setOnItemClickListener { adapter, view, position ->
            startActivity(Intent(this@CarSelfActivity, CarCostActivity::class.java).apply {
                putExtra("Id", mAdapter.data[position].id)
                putExtra("Type", "3")
            })
        }


        viewModel.mGetCarPlanList.observe(this, {
            if (it.isSuccess()) {
                if (it.data.receiveWarePlanList.isNullOrEmpty()) {
                    ToastUtils.showShort("没有数据")
                    mAdapter.data.clear()
                    mAdapter.notifyDataSetChanged()
                }else{
                    mAdapter.data.clear()
                    mAdapter.data.addAll(it.data.receiveWarePlanList)
                    mAdapter.notifyDataSetChanged()
                }
            }
        })
    }


    private fun showTimeListDialog() {
        val list = listOf(
            "近一天",
            "近三天",
            "近七天",
        )

        MaterialDialog(this).show {
            listItems(items = list) { _, index, text ->
                this@CarSelfActivity.tv_car_time.text = text
                mDay = when (index) {
                    0 -> {
                        "1"
                    }
                    1 -> {
                        "3"
                    }
                    else -> {
                        "7"
                    }
                }

                val params = Param::class.java.newInstance()
                params.day = mDay
                viewModel.getCarPlanList(params)
            }
            lifecycleOwner(this@CarSelfActivity)
        }
    }

}